package com.wolfpire.section.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.wolfpire.chapter.dao.ChapterDao;
import com.wolfpire.chapter.dao.SubjectChapterRelationDao;
import com.wolfpire.profession.dao.ProfessionDao;
import com.wolfpire.section.dao.ChapterSectionRelationDao;
import com.wolfpire.section.dao.MyDownLoadHistoryDao;
import com.wolfpire.section.dao.SectionDao;
import com.wolfpire.section.dao.StudyHistoryDao;
import com.wolfpire.section.service.MyDownLoadHistoryService;
import com.wolfpire.section.service.SectionService;
import com.wolfpire.section.service.StudyHistoryService;
import com.wolfpire.subject.dao.ProfessionSubjectRelationDao;
import com.wolfpire.subject.dao.SubjectDao;
import com.wolfpire.subject.service.SubjectService;
import com.wolfpire.system.common.Constants;
import com.wolfpire.system.common.MD5;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.base.dao.IBaseHibernateDao;
import com.wolfpire.system.common.base.service.impl.BaseHibernateService;
import com.wolfpire.system.common.util.StringUtils;
import com.wolfpire.system.dao.AuthorityDao;
import com.wolfpire.system.dao.OrgDao;
import com.wolfpire.system.dao.OrgUserRelationDao;
import com.wolfpire.system.dao.RoleDao;
import com.wolfpire.system.dao.UserDao;
import com.wolfpire.system.dao.UserRoleRelationDao;
import com.wolfpire.system.model.Attachment;
import com.wolfpire.system.model.Authority;
import com.wolfpire.system.model.Chapter;
import com.wolfpire.system.model.ChapterSectionRelation;
import com.wolfpire.system.model.MyDownLoadHistory;
import com.wolfpire.system.model.Org;
import com.wolfpire.system.model.OrgUserRelation;
import com.wolfpire.system.model.Profession;
import com.wolfpire.system.model.ProfessionSubjectRelation;
import com.wolfpire.system.model.Section;
import com.wolfpire.system.model.StudyHistory;
import com.wolfpire.system.model.Subject;
import com.wolfpire.system.model.SubjectChapterRelation;
import com.wolfpire.system.model.User;
import com.wolfpire.system.model.UserRoleRelation;
import com.wolfpire.system.service.AttachmentService;
import com.wolfpire.system.service.UserService;

@Service("myDownLoadHistoryService")
public class MyDownLoadHistoryServiceImpl extends BaseHibernateService<MyDownLoadHistory, Long> implements
	MyDownLoadHistoryService {
	
	@Resource private MyDownLoadHistoryDao myDownLoadHistoryDao;
	@Resource private SectionDao sectionDao;
	@Resource private ChapterSectionRelationDao chapterSectionRelationDao;
	@Resource private ChapterDao chapterDao;
	@Resource private SubjectChapterRelationDao subjectChapterRelationDao;
	@Resource private SubjectDao subjectDao;
	public List<MyDownLoadHistory> list(MyDownLoadHistory entity) {
		// TODO Auto-generated method stub
		return null;
	}
	/**
	 * 保存我的下载
	 */
	public void saveMyDownLoadHistory(long userId, long sectionId, String filePath) {
		Section section = this.sectionDao.get(sectionId);
		ChapterSectionRelation csr = null;
		SubjectChapterRelation scr = null;
		Chapter chapter = null;
		Subject subject = null;
		if(section != null)
			csr = this.chapterSectionRelationDao.getCSRelation(sectionId);
		if(csr != null)
			chapter = this.chapterDao.get(csr.getChapterId());
		if(chapter != null)
			scr = this.subjectChapterRelationDao.getSCRelation(chapter.getId());
		if(scr != null)
			subject = this.subjectDao.get(scr.getSubjectId());
		
		
		MyDownLoadHistory myDownLoadHistory = new MyDownLoadHistory(userId, subject.getId(), sectionId, 
				subject.getName(), chapter.getName(), section.getName(), filePath, new Date());
		
		this.myDownLoadHistoryDao.save(myDownLoadHistory);
	}
	public List<MyDownLoadHistory> findMyDownLoadHistory(long userId, long subjectId) {
		// TODO Auto-generated method stub
		return this.myDownLoadHistoryDao.findMyDownLoadHistory(userId, subjectId);
	}
	@Override
	protected IBaseHibernateDao<MyDownLoadHistory, Long> getEntityDao() {
		// TODO Auto-generated method stub
		return this.myDownLoadHistoryDao;
	}
	public List<MyDownLoadHistory> findMySubject(long userId) {
		// TODO Auto-generated method stub
		return this.myDownLoadHistoryDao.findMySubject(userId);
	}
	
}
